package my_web.service;
import java.util.ArrayList;
import java.util.List;
import dao.dao.Dao;
import dao.dao.PreSQL;
import dao.dao.impl.DaoFactory;
import my_web.Interface.DepInterface;
import my_web.bean.Dep;
import my_web.model.DepDto;
import my_web.page.Page;
import my_web.page.PageEngine;
import my_web.page.PageParam;
import my_web.page.Imp.PageEngineFactory;
import my_web.page.Imp.PageImp;
public class DepService implements DepInterface {
	public List<Dep>getAllDeps(){
		Dao dao=DaoFactory.getCurrDao();
		return dao.queryBeanList(Dep.class, "select *from dep_table order by d_id");	 
	}
	
	public Object getDepPage(PageParam param) {
		Dao dao=DaoFactory.getCurrDao();
			return new PageImp(new PageEngine(){
				public int getTotal() throws Exception {
					return dao.queryInteger("select count(d_id) from dep_table");
				}
				public List<?> getReclist(int start, int end) throws Exception {
					String sql=
							"select *from "
							+ "(select D.*,rownum row_ from dep_table D where rownum<="+end+")t1 "
							+ "where row_>"+start;
					return dao.queryBeanList(Dep.class, sql);
				}
			},
				param
			);
	}
	
	public int addDep(DepDto dto) {
		Dao dao=DaoFactory.getCurrDao();
		return dao.update("insert into dep_table(d_id,d_name,d_state) values(?,?,?)",dto.getD_id(),dto.getD_name(),dto.getD_state());
	}
	
	public void removeDepById(String d_id) {
		Dao dao=DaoFactory.getCurrDao();
		dao.update("delete from dep_table where d_id=?",d_id);
	}
	
	public void removeDepById(String[] d) {
		if(d==null)return;
		Dao dao=DaoFactory.getCurrDao();
		PreSQL psql=dao.preSql("delete from dep_table where d_id=?");
		for(String b_id:d){
			psql.update(b_id);
		}
	}
	
	public Dep getDepById(String d) {
		Dao dao=DaoFactory.getCurrDao();
		return dao.queryBean(Dep.class,"select *from dep_table where d_id=?", d);	
	}
	
	public void modifyDep(DepDto dto) {
		Dao dao=DaoFactory.getCurrDao();
		dao.update("update dep_table set d_name=?,d_state=?where d_id=?",dto.getD_name(),dto.getD_state(),dto.getD_id());
	}
	
	public List<Dep> getAllDeps(DepDto dto) {
		Dao dao=DaoFactory.getCurrDao();
		String sql="select d_id,d_name,d_state from dep_table where 1=1";
		StringBuilder where=new StringBuilder();
		List<Object> paramList=new ArrayList<>();
		if(dto!=null){
			if(dto.getD_id()!=null && !dto.getD_id().equals("")){
				where.append(" and d_id like ?");
				paramList.add("%"+dto.getD_id()+"%");
			}
			if(dto.getD_name()!=null && !dto.getD_name().equals("")){
				where.append(" and d_name like ?");
				paramList.add("%"+dto.getD_name()+"%");
			}
			if(dto.getD_state()!=null && !dto.getD_state().equals("")){
				where.append(" and d_state like ?");
				paramList.add("%"+dto.getD_state()+"%");
			}	
		}
			return dao.queryBeanList(Dep.class,sql+where);
	}
	
	public Page DynamicQueryAllDeps(DepDto dto){
		Dao dao=DaoFactory.getCurrDao();
		/**
		 * 1:获取查询条件对应的完整sql语句
		 */
		String sql="select d_id,d_name,d_state from dep_table where 1=1";
		StringBuilder where=new StringBuilder();
		List<Object> paramList=new ArrayList<>();
		if(dto!=null){
			if(dto.getD_id()!=null && !dto.getD_id().equals("")){
				where.append(" and d_id like ?");
				paramList.add("%"+dto.getD_id()+"%");
			}
			if(dto.getD_name()!=null && !dto.getD_name().equals("")){
				where.append(" and d_name like ?");
				paramList.add("%"+dto.getD_name()+"%");
			}
			if(dto.getD_state()!=null && !dto.getD_state().equals("")){
				where.append(" and d_state like ?");
				paramList.add("%"+dto.getD_state()+"%");
			}	
		}
		where=where.append("order by d_id");
		/**
		 * 2.通过匿名类的方式创建PageImp-分页实例
		 */
		/*
		new PageImp(
			new PageEngine(){
				public int getTotal() throws Exception {
					return dao.queryInteger("select count(d_id) from dep_table");
				}
				public List<?> getReclist(int start, int end) throws Exception {
					String sql=
							"select *from "
							+ "(select D.*,rownum row_ from "
							+ "(sql1+ where) t0"
							+ "where rownum<="+end+")t1 "
							+ "where row_>"+start;
					return dao.queryBeanList(Dep.class, sql);
				}
			},
			param
		);
		*/
		try{
			return PageEngineFactory.createOraclePage(Dep.class, dao, sql+where, dto, paramList.toArray());
		}catch(RuntimeException e){
			throw e;
		}
	}
}
